package ch.zhaw.cctd.domain.match;

import java.util.LinkedList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.zhaw.cctd.logic.network.RemoteClient;

/**
 * Speichert alle RemoteClients in einer Liste. Die Klasse bietet die Methode
 * getFirstFreeIndex um die erste freie ID zu finden, die als ClientID verwendet
 * werden kann
 * 
 * @author Rolf Koch
 * 
 * @param <T>
 */
public class ClientList<T extends RemoteClient> extends LinkedList<T> {

	/**
	 * Serializable ID
	 */
	private static final long serialVersionUID = -4304806549141589457L;

	/**
	 * Logger
	 */
	private static final Logger logger = LoggerFactory.getLogger(MatchServer.class);

	/**
	 * Default Constructor
	 */
	public ClientList() {
		super();
	}

	/**
	 * Returns the first free ID of the Table as ClientID
	 * @return
	 */
	public int getFirstFreeIndex() {
		logger.trace("Current Table length is {} !", size());
		for (int i = 0; i < size(); i++) {
			if (this.get(i) == null) {
				logger.trace("Next Free ID is   {} !", i);
				return i;
			}
		}
		return size();
	}

	public int getClientCount() {
		int count = 0;
		for(int i = 0; i< size(); i++) {
			if(this.get(i) != null) {
				count ++;
			}
		}
		return count;
	}
}